/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package LoadingDialog;

import java.awt.Desktop.Action;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.UIManager;

/**
 *
 * @author comiang
 */
public class ModalProgressDemo{ 
    static JFrame frame; 
    static Runnable heavyRunnable = new Runnable(){ 
        public void run(){ 
            ProgressMonitor monitor = ProgressUtil.createModalProgressMonitor(frame, 100, false, 1000); 
            monitor.start("Fetching 1 of 10 records from database..."); 
            try{ 
                for(int i=0; i<100; i++){ 
                    fetchRecord(i); 
                    monitor.setCurrent("Fetching "+(i+1)+" of 100 records from database", (i+1)); 
                   
                } 
            } finally{ 
                // to ensure that progress dlg is closed in case of any exception 
                if(monitor.getCurrent()!=monitor.getTotal()) 
                    
                    monitor.setCurrent(null, monitor.getTotal()); 
               
            } 
            heavyAction.setEnabled(true); 
            
        } 
 int longx=25;boolean chk=true;
        private void fetchRecord(int index){ 
            System.out.println(longx+" "+chk);
            try{ 
                if(chk==false){
                    longx=longx-5;
                }else if(longx<300&&chk==true){
                    longx=longx+5;
                     //chk=false;
                }
                if(longx==300){
                   
                    chk=false;
                }
                Thread.sleep(longx); 
            } catch(InterruptedException e){ 
                e.printStackTrace(); 
            } 
        } 
    }; 
 
    static AbstractAction heavyAction = new AbstractAction("Databse Query"){ 
        public void actionPerformed(ActionEvent e){ 
            setEnabled(false); 
            new Thread(heavyRunnable).start(); 
        } 
    }; 
 static Thread  TH;
    public static void main(String args[]){ 
        try{ 
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
        } catch(Exception e){ 
            e.printStackTrace(); 
        } 
        frame = new JFrame("Modal Progress Dialog - santhosh@in.fiorano.com"); 
        frame.getContentPane().setLayout(new FlowLayout()); 
        frame.getContentPane().add(new JButton(heavyAction)); 
        frame.setSize(300, 200); 
        frame.setLocationRelativeTo(null); 
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        frame.setVisible(false); 
      
      
    } 
    
    public void showProgress(){
        TH=  new Thread(heavyRunnable);
      TH.start();
    }
} 


